package kr.co.doriclassic.chapter27;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class SQLSelectTest {
	public static void main(String[] args) throws SQLException {
		Connection conn = makeConnect();
		Statement stmt = conn.createStatement();
		//쿼리 날리기
		ResultSet rs = stmt.executeQuery("select * from customers");
		//쿼리 실행
		while(rs.next()){
			int id = rs.getInt("cust_id");
			String name = rs.getString("cust_name");
			System.out.println(id + " " + name);
		}
	}

	private static Connection makeConnect() {
		String url = "jdbc:mysql://localhost/mysql_study";
		String id = "root";
		String pw = "1111";
		Connection conn=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			System.out.println("드라이버 적재 성공");
			conn = DriverManager.getConnection(url, id, pw);
			System.out.println("데이터베이스 연결 성공");
		} catch (ClassNotFoundException e) {
			System.out.println("드라이버를 찾을 수 없습니다.");
		} catch (SQLException e) {
			System.out.println("DB에 연결할 수 없습니다.");
		}
		
		return conn;
	}
}
